package vietnam.traveltools;

import vietnam.traveltools.adapter.TabsPagerAdapter;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements TabListener {
	private ViewPager viewPager;
	private TabsPagerAdapter mAdapter;
	public ActionBar actionBar;
	private String[] tabs = { "About", "Main", "Search", "Maps" }; // About ==
																	// Settings
	// private String[] tabs = { "About", "Main"};
	final int[] ICONS = new int[] { R.drawable.tab_settings,
			R.drawable.tab_main, R.drawable.tab_search, R.drawable.tab_maps, };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.main_activity);

		try {
			setupUI(findViewById(R.id.pager));
			getActionBar().setDisplayHomeAsUpEnabled(true);
			viewPager = (ViewPager) findViewById(R.id.pager);
			actionBar = getActionBar();
			mAdapter = new TabsPagerAdapter(getFragmentManager());
			actionBar.setHomeButtonEnabled(false);
			viewPager.setAdapter(mAdapter);
			actionBar.setDisplayShowHomeEnabled(false);
			actionBar.setDisplayShowTitleEnabled(false);
			actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Set
																			// actionbar
																			// thành
																			// tab

			// Add Tabs
			for (int i = 0; i < tabs.length; i++) {
				actionBar.addTab(actionBar.newTab()
						.setIcon(getResources().getDrawable(ICONS[i]))
						.setTabListener(this));
			}

			viewPager
					.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

						@Override
						public void onPageSelected(int position) {
							actionBar.setSelectedNavigationItem(position);

						}

						@Override
						public void onPageScrolled(int arg0, float arg1,
								int arg2) {
						}

						@Override
						public void onPageScrollStateChanged(int arg0) {
						}
					});
			actionBar.setSelectedNavigationItem(1);

		} catch (Exception e) {
			System.out
					.print("Có vẻ như đã bị lỗi, hãy chụp lại màn hình và gửi cho chúng tôi");
			Toast.makeText(getApplicationContext(), "Lỗi: " + e,
					Toast.LENGTH_LONG).show();

		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			AlertDialog.Builder onBack = new AlertDialog.Builder(this);

			onBack.setMessage("Bạn có chắc chắc muốn thoát?");
			onBack.setPositiveButton("Thoát",
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							System.exit(0);
						}
					});
			onBack.setNegativeButton("Trở về",
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							return;
						}
					});
			onBack.show();
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		viewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}
	
	public static void hideSoftKeyboard(Activity activity) {
		InputMethodManager inputMethodManager = (InputMethodManager) activity
				.getSystemService(Activity.INPUT_METHOD_SERVICE);
		inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus()
				.getWindowToken(), 0);
	}
	public void setupUI(View view) {

		// Set up touch listener for non-text box views to hide keyboard.
		if (!(view instanceof EditText)) {

			view.setOnTouchListener(new View.OnTouchListener() {

				public boolean onTouch(View v, MotionEvent event) {
					hideSoftKeyboard(MainActivity.this);
					return false;
				}

			});
		}

		// If a layout container, iterate over children and seed recursion.
		if (view instanceof ViewGroup) {

			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

				View innerView = ((ViewGroup) view).getChildAt(i);

				setupUI(innerView);
			}
		}
	}


	// @Override
	// public boolean onOptionsItemSelected(MenuItem item) {
	// // Handle action bar item clicks here. The action bar will
	// // automatically handle clicks on the Home/Up button, so long
	// // as you specify a parent activity in AndroidManifest.xml.
	// int id = item.getItemId();
	// if (id == R.id.action_settings) {
	// return true;
	// }
	// return super.onOptionsItemSelected(item);
	// }

	/**
	 * A placeholder fragment containing a simple view.
	 */
	// public static class PlaceholderFragment extends FragmentActivity
	// implements
	// ActionBar.TabListener {
	//
	// public PlaceholderFragment() {
	// }
	//
	// public View onCreateView(LayoutInflater inflater, ViewGroup container,
	// Bundle savedInstanceState) {
	// View rootView = inflater.inflate(R.layout.main_fragment, container,
	// false);
	// return rootView;
	// }

}
